package com.hyg.lib_common.SQLite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class MyDataBaseHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "UserData";                       //数据库名称

    private static final String TABLE_UserInfo = "UserInfo";                //用户信息表
    private static final String TABLE_ReportList = "ReportList";            //报告列表
    private static final String TABLE_MedicalInfo = "MedicalInfo";          //报告详情表
    private static final String TABLE_Tongue = "Tongue";                    //望诊表
    private static final String TABLE_Pulse  = "Pulse";                     //脉诊表
    private static final String TABLE_PulseData = "PulseData";              //脉诊详情表
    private static final String TABLE_EarData = "EarData";                  //耳电表
    private static final String TABLE_Body = "Body";                        //体穴表
    private static final String TABLE_IntroductionHealth = "IntroductionHealth";//楔子养生
    private static final String TABLE_MotionHealth = "MotionHealth";            //运动养生
    private static final String TABLE_SeasonHealth = "SeasonHealth";            //应季养生
    private static final String TABLE_DietHealth = "DietHealth";                //膳食养生
    private static final String TABLE_MedicineBathHealth = "MedicineBathHealth";//药浴养生
    private static final String TABLE_TeaHealth = "TeaHealth";                  //茶饮养生
    private static final String TABLE_MoxibustionHealth  = "MoxibustionHealth"; //艾灸养生
    private static final String TABLE_ResultAnalysisInfo  = "ResultAnalysisInfo";//结果解析

    private static final int DB_VERSION = 2;
    private static final String TAG = "MyDataBaseHelper";

    public MyDataBaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO 创建数据库后，对数据库的操作
        ResetAllTable(db);

        Log.e(TAG, DB_CREATE_TABLE_UserInfo);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO 更改数据库版本的操作
        if(oldVersion < DB_VERSION) {
//            db.execSQL("ALTER TABLE pedant ADD COLUMN student_name text");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_UserInfo + ";");
            db.execSQL(DB_CREATE_TABLE_UserInfo);
        }
        // 1 -> 2 版本数据库的改动：修改头像，添加字段
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        // TODO 每次成功打开数据库后首先被执行
//        ResetAllTable(db);
        Log.i(TAG, "MyDataBaseHelper onOpen");
        super.onOpen(db);

    }

//    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion){
        // TODO 每次成功打开数据库后数据库版本降级时将被执行，不重写的话将会崩溃
//        super.onDowngrade(db, oldVersion, newVersion);
//    }

    public void onReset(SQLiteDatabase db) {
        // TODO 每次成功打开数据库后首先被执行
        ResetAllTable(db);
        Log.i(TAG, "MyDataBaseHelper onReset");
        super.onOpen(db);
    }

    private static final String DB_CREATE_TABLE_UserInfo = "CREATE TABLE " + TABLE_UserInfo + " ("
            + "UserInfoId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "userId" + " TEXT ,"
            + "realName" + " TEXT,"
            + "gender" + " TEXT,"
            + "idNumber" + " TEXT,"
            + "socialId" + " TEXT,"
            + "address" + " TEXT,"
            + "marryInfo" + " TEXT,"
            + "historyInfo" + " TEXT,"
            + "operationInfo" + " TEXT,"
            + "allergyInfo" + " TEXT,"
            + "familyInfo" + " TEXT,"
            + "tasteInfo" + " TEXT,"
            + "headImage" + " TEXT,"
            + "createTime" + " TEXT,"
            + "updateTime" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_ReportList = " CREATE TABLE "+ TABLE_ReportList +" ("
            + "ReportListId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "patientName" + " TEXT ,"
            + "diagnosticMechanismName" + " TEXT,"
            + "createTime" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_MedicalInfo= "CREATE TABLE " + TABLE_MedicalInfo + " ("
            + "MedicalId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "patientName" + " TEXT,"
            + "patientSex" + " TEXT,"
            + "patientAge" + " TEXT,"
            + "patientBirth" + " TEXT,"
            + "patientPhone" + " TEXT,"
            + "patientCardNum" + " TEXT,"
            + "patientSSCard" + " TEXT,"
            + "patientMedicalCard" + " TEXT,"
            + "patientJob" + " TEXT,"
            + "patientArea" + " TEXT,"
            + "patientNation" + " TEXT,"
            + "patientSegment" + " TEXT,"
            + "patientEatingHabits" + " TEXT,"
            + "diagnosticMechanismID" + " TEXT,"
            + "mainComplaint" + " TEXT,"
            + "patientFingerPrint" + " TEXT,"
            + "pastMedical" + " TEXT,"
            + "nowMedical" + " TEXT,"
            + "familyMedicalHistory" + " TEXT,"
            + "drugAllergyHistory" + " TEXT,"
            + "lastDate" + " TEXT,"
            + "isPrint" + " TEXT,"
            + "barCode" + " TEXT,"
            + "doctorId" + " TEXT,"
            + "approvalDocID" + " TEXT,"
            + "managerDocID" + " TEXT,"
            + "tongueID" + " TEXT,"
            + "smellID" + " TEXT,"
            + "pulseID" + " TEXT,"
            + "earID" + " TEXT,"
            + "bodyID" + " TEXT,"
            + "physiqueType" + " TEXT,"
            + "physicalID" + " TEXT,"
            + "reMarks" + " TEXT,"
            + "customerLevel" + " TEXT,"
            + "createTime" + " TEXT,"
            + "updateTime" + " TEXT,"
            + "spareReportID0" + " TEXT,"
            + "spareReportID1" + " TEXT,"
            + "spareReportID2" + " TEXT,"
            + "spaerCol0" + " TEXT,"
            + "spaerCol1" + " TEXT,"
            + "spaerCol2" + " TEXT,"
            + "dietInfo" + " TEXT,"
            + "medicineInfo" + " TEXT,"
            + "teaInfo" + " TEXT,"
            + "moxibustionHealth" + " TEXT,"
            + "approvalDoctor" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_Tongue = "CREATE TABLE " + TABLE_Tongue + " ("
            + "TongueId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "doctorID" + " TEXT,"
            + "medicalID" + " TEXT,"
            + "tongueImagePath" + " TEXT,"
            + "faceImagePath" + " TEXT,"
            + "tongueVesselImagePath" + " TEXT,"
            + "tongueSpirit" + " TEXT,"
            + "tongueShape" + " TEXT,"
            + "tongueState" + " TEXT,"
            + "tongueColorType" + " TEXT,"
            + "tongueCoatColorType" + " TEXT,"
            + "tongueCoatType" + " TEXT,"
            + "tongueCoatColor" + " TEXT,"
            + "tongueCoatArea" + " TEXT ,"
            + "tongueColorRGB" + " TEXT,"
            + "tongueCrackNum" + " TEXT,"
            + "tonguePrickNum" + " TEXT,"
            + "tonguePrickArea" + " TEXT,"
            + "tongueEcchymosisNum" + " TEXT,"
            + "tongueToothMarks" + " TEXT,"
            + "doctorDiagnosis" + " TEXT,"
            + "doctorYaoShan" + " TEXT,"
            + "tongueZDImgPath" + " TEXT,"
            + "tongueRemarks" + " TEXT,"
            + "tongueMailuo" + " TEXT,"
            + "tongueSP" + " TEXT ,"
            + "sPDate" + " TEXT,"
            + "faceColor" + " TEXT,"
            + "faceSebuColor" + " TEXT,"
            + "faceDiagnosis" + " TEXT,"
            + "renZhongColor" + " TEXT,"
            + "renZhongXingTai" + " TEXT,"
            + "lipColor" + " TEXT,"
            + "lipColorType" + " TEXT,"
            + "noseColor" + " TEXT,"
            + "noseColorType" + " TEXT,"
            + "eyeWhiteColor" + " TEXT,"
            + "eyePouchColor" + " TEXT,"
            + "createTime" + " TEXT,"
            + "updateTime" + " TEXT,"
            + "tongueIDS" + " TEXT ,"
            + "spaerCol0" + " TEXT,"
            + "spaerCol1" + " TEXT,"
            + "spaerCol2" + " TEXT,"
            + "diagnosisDate" + " TEXT,"
            + "spdoctorID" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_Pulse = "CREATE TABLE " + TABLE_Pulse + " ("
            + "pulseId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "doctorID" + " TEXT,"
            + "spDoctorID" + " TEXT,"
            + "medicalID" + " TEXT ,"
            + "pulseZDImgpath" + " TEXT,"
            + "diagnosisDate" + " TEXT,"
            + "pulseZhenduan" + " TEXT,"
            + "pulseYaoshan" + " TEXT,"
            + "leftType" + " TEXT,"
            + "rightType" + " TEXT,"
            + "remarks" + " TEXT,"
            + "spaerCol0" + " TEXT,"
            + "spaerCol1" + " TEXT,"
            + "spaerCol2" + " TEXT,"
            + "sPDate" + " TEXT,"
            + "createTime" + " TEXT,"
            + "updateTime" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_PulseData = "CREATE TABLE " + TABLE_PulseData + " ("
            + "PulseDataId" + " INTEGER PRIMARY KEY ,"
            + "pulseId" + " TEXT ,"
            + "pulseHand" + " TEXT,"
            + "pulsePos" + " TEXT,"
            + "pulsePressure" + " TEXT,"
            + "pulsePressureType" + " TEXT,"
            + "data0" + " TEXT,"
            + "data1" + " TEXT,"
            + "data2" + " TEXT,"
            + "startIndex" + " TEXT,"
            + "endIndex" + " TEXT,"
            + "period" + " TEXT,"
            + "fluency" + " TEXT,"
            + "tensionDegree" + " TEXT,"
            + "pulseForce" + " TEXT,"
            + "pulseRate" + " TEXT,"
            + "pulseType" + " TEXT,"
            + "remarks" + " TEXT,"
            + "diagnosisDate" + " TEXT,"
            + "createTime" + " TEXT,"
            + "updateTime" + " TEXT,"
            + "spaerCol0" + " TEXT,"
            + "spaerCol1" + " TEXT,"
            + "spaerCol2" + " TEXT,"
            + "mwap" + " TEXT,"
            + "mwup" + " TEXT,"
            + "mwia" + " TEXT,"
            + "pwa" + " TEXT,"
            + "mwh" + " TEXT,"
            + "twp" + " TEXT,"
            + "twh" + " TEXT,"
            + "pfu" + " TEXT,"
            + "sru" + " TEXT,"
            + "rwp" + " TEXT,"
            + "rwh" + " TEXT,"
            + "tws" + " TEXT,"
            + "rws" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_Body = "CREATE TABLE " + TABLE_Body + " ("
            + "BodyId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "MedicalID" + " TEXT ,"
            + "DoctorID" + " TEXT,"
            + "DiagnosisPath" + " TEXT,"
            + "AcuPointImpedance" + " TEXT,"
            + "DiagnosisDate" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_EarData = "CREATE TABLE " + TABLE_EarData + " ("
            + "EarId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "medicalID" + " TEXT ,"
            + "doctorId" + " TEXT,"
            + "basicImpedance" + " TEXT,"
            + "acuPointImpedance" + " TEXT,"
            + "diagnosisPath" + " TEXT,"
            + "reMarks" + " TEXT,"
            + "diagnosisDate" + " TEXT,"
            + "createTime" + " TEXT,"
            + "updateTime" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_IntroductionHealth  = "CREATE TABLE " + TABLE_IntroductionHealth  + " ("
            + "IntroductionHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "IntroductionContent" + " TEXT ,"
            + "IntroductionImages" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_MotionHealth   = "CREATE TABLE " + TABLE_MotionHealth  + " ("
            + "MotionHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "SPName" + " TEXT ,"
            + "SPContent" + " TEXT,"
            + "SPTag" + " TEXT,"
            + "SPImages" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_SeasonHealth  = "CREATE TABLE " + TABLE_SeasonHealth + " ("
            + "SeasonHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "SeasonContent" + " TEXT ,"
            + "SeasonImages" + " TEXT,"
            + "Tag" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_DietHealth  = "CREATE TABLE " + TABLE_DietHealth + " ("
            + "DietHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "DietName" + " TEXT ,"
            + "DietMaterial" + " TEXT,"
            + "DietMaking" + " TEXT,"
            + "DietEfficacy" + " TEXT,"
            + "DietTaboo" + " TEXT,"
            + "DietImages" + " TEXT,"
            + "Tag" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_MedicineBathHealth = "CREATE TABLE " + TABLE_MedicineBathHealth  + " ("
            + "MedicineBathHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "MedicineName" + " TEXT ,"
            + "MedicineMaterial" + " TEXT,"
            + "MedicineUsage" + " TEXT,"
            + "MedicineCure" + " TEXT,"
            + "MedicineTaboo" + " TEXT,"
            + "MedicineImages" + " TEXT,"
            + "Tag" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_TeaHealth = "CREATE TABLE " + TABLE_TeaHealth  + " ("
            + "TeaHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "TeaName" + " TEXT ,"
            + "TeaMaterial" + " TEXT,"
            + "TeaMaking" + " TEXT,"
            + "TeaEfficacy" + " TEXT,"
            + "TeaTaboo" + " TEXT,"
            + "TeaImages" + " TEXT,"
            + "Tag" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_MoxibustionHealth = "CREATE TABLE " + TABLE_MoxibustionHealth + " ("
            + "MoxibustionHealthId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "MoxibustionName" + " TEXT ,"
            + "MoxibustionPosition" + " TEXT,"
            + "MoxibustionDescription" + " TEXT,"
            + "Moxibustion" + " TEXT,"
            + "MoxibustionEfficacy" + " TEXT,"
            + "MoxibustionImages" + " TEXT,"
            + "Tag" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private static final String DB_CREATE_TABLE_ResultAnalysisInfo = "CREATE TABLE " + TABLE_ResultAnalysisInfo + " ("
            + "ResultAnalysisInfoId" + " INTEGER PRIMARY KEY NOT NULL,"
            + "ResultName" + " TEXT ,"
            + "ResultInfo" + " TEXT,"
            + "ResultFenlei" + " TEXT,"
            + "Tag" + " TEXT,"
            + "ReMarks" + " TEXT"
            + ");";

    private void insert(SQLiteDatabase db){
        //插入数据SQL语句
        String stu_sql="insert into stu_table(sname,snumber) values('xiaoming','01005')";
        //执行SQL语句
        db.execSQL(stu_sql);
    }
    private void delete(SQLiteDatabase db) {
        //删除SQL语句
        String sql = "delete from stu_table where _id = 6";
        //执行SQL语句
        db.execSQL(sql);
    }
    private void update(SQLiteDatabase db){
        //修改SQL语句
        String sql = "update stu_table set snumber = 654321 where id = 1";
        //执行SQL
        db.execSQL(sql);
    }
    private void query(SQLiteDatabase db) {
        //查询获得游标
        Cursor cursor = db.query ("MedicalRecord",null,null,null,null,null,null);
        //判断游标是否为空
        if(cursor.moveToFirst()) {
            //遍历游标
            for(int i=0;i<cursor.getCount();i++){
                cursor.move(i);
                //获得ID
                int id = cursor.getInt(0);
                //获得用户名
                String username = cursor.getString(1);
                //获得密码
                String password = cursor.getString(2);
                int age = cursor.getInt(3);
//                Log.d("MyDataBaseHelper",id + ":" + username + ":" + password + ":" +age);
            }
        }
    }
    private void drop(SQLiteDatabase db){
        //删除表的SQL语句
        String sql ="DROP TABLE stu_table";
        //执行SQL
        db.execSQL(sql);
    }



    public void ResetAllTable(SQLiteDatabase db){

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_UserInfo + ";");
        db.execSQL(DB_CREATE_TABLE_UserInfo);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ReportList + ";");
        db.execSQL(DB_CREATE_TABLE_ReportList);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MedicalInfo + ";");
        db.execSQL(DB_CREATE_TABLE_MedicalInfo);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_Tongue + ";");
        db.execSQL(DB_CREATE_TABLE_Tongue);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_Pulse + ";");
        db.execSQL(DB_CREATE_TABLE_Pulse);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PulseData + ";");
        db.execSQL(DB_CREATE_TABLE_PulseData);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_Body + ";");
        db.execSQL(DB_CREATE_TABLE_Body);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EarData + ";");
        db.execSQL(DB_CREATE_TABLE_EarData);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_IntroductionHealth + ";");
        db.execSQL(DB_CREATE_TABLE_IntroductionHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MotionHealth + ";");
        db.execSQL(DB_CREATE_TABLE_MotionHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SeasonHealth + ";");
        db.execSQL(DB_CREATE_TABLE_SeasonHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_DietHealth + ";");
        db.execSQL(DB_CREATE_TABLE_DietHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MedicineBathHealth + ";");
        db.execSQL(DB_CREATE_TABLE_MedicineBathHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TeaHealth + ";");
        db.execSQL(DB_CREATE_TABLE_TeaHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MoxibustionHealth + ";");
        db.execSQL(DB_CREATE_TABLE_MoxibustionHealth);

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ResultAnalysisInfo + ";");
        db.execSQL(DB_CREATE_TABLE_ResultAnalysisInfo);
    }
}
